const userModel = require('../../models/userModel');
const roleModel = require('../../models/roleModel');
//读取配置项
module.exports = (req,res,next) =>{
  //校验token

  userModel.findOne({user:req.session.user}).then(data=>{
    if(data){
        roleModel.findOne({id:data.permissions}).then(data=>{
            if(data){
                if(data.name=="admin"){
                    //管理员权限通过
                    next()
                }else{
                    return res.json({
                        code:"0",
                        msg:"用户无此权限",
                        data:null
                    })
                }
             
            }else{
                return res.json({
                    code:"0",
                    msg:"用户无此权限",
                    data:null
                })
            }

        }).catch(err=>{
            return res.json({
                code:"0",
                msg:"用户无此权限",
                data:null
            })
        })

     
    }else{
        return res.json({
            code:"0",
            msg:"操作错误",
            data:null
        })
    }
  }).catch(err=>{
    return res.json({
        code:"0",
        msg:"用户无此权限",
        data:null
    })
  })
  
}